package com.androidprueba1;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import com.ims.metnum.lineal.Jacobi;
import com.ims.metnum.lineal.exceptions.EndConditionIsLessThanOrEqualZero;
import com.ims.metnum.lineal.exceptions.EndConditionIsLessThanSignificantDigitsException;
import com.ims.metnum.lineal.exceptions.MatrixDimensionsException;
import com.ims.metnum.lineal.exceptions.MatrixNotSquareException;
import com.ims.metnum.lineal.exceptions.MaximumNumberOfIterationsLessThan1Exception;
import com.ims.metnum.lineal.exceptions.MethodIsNotConvergentException;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    
	public static double[][] matrixA = { { -1,2}, {1,-4} };
	
	public static double[] matrixX0 = {1,-2};
	
	public static double[] matrixB = {1,0};
	
	public static double endCondition = 0.00001;
	
	public static int maxIterationNumber = 30;
	
	public static String salida="";
	
	public static int significantDigits = 6;
	
	public static String formatDoubleArray(double[] array){
		String result = "[ ";
		for(int i = 0 ; i<array.length ; i++){
			result += (i<array.length-1)? (array[i] + " , "):array[i];
		}
		result += " ]";
		return result;
	}
	
	public static String formatDoubleMatrix(double[][] matrix){
		String result = "[ ";
		for(int i = 0 ; i<matrix.length ; i++){
			result += "(";
			for( int j = 0; j<matrix[i].length; j++){
				result += (j<matrix[i].length-1)? (matrix[i][j] + " , "):matrix[i][j];
			}
			result += i < matrix.length - 1 ? "), " : ")";
		}
		result += " ]";
		return result;
	}
	
	public static void jacobi() throws MethodIsNotConvergentException, MaximumNumberOfIterationsLessThan1Exception, EndConditionIsLessThanOrEqualZero, MatrixNotSquareException, MatrixDimensionsException, EndConditionIsLessThanSignificantDigitsException {
		Jacobi jacobiMethod = new Jacobi(MainActivity.matrixA, MainActivity.matrixB, MainActivity.matrixX0, MainActivity.endCondition, MainActivity.maxIterationNumber, MainActivity.significantDigits);
		jacobiMethod.run();
		salida = "divergence condition = " + jacobiMethod.satisfiesDivergenceCondition() + "\n";
		salida += "convergence condition = " + jacobiMethod.satisfiesConvergenceCondition() + "\n";
		salida += "A = " + formatDoubleMatrix(jacobiMethod.getMatrixA()) + "\n";
		salida +="B = " + formatDoubleArray(jacobiMethod.getMatrixB()) + "\n";
		
		for(int n = 0; n < jacobiMethod.getIterationsResults().size(); n++){
			salida += "X" + n + " = " + formatDoubleArray(jacobiMethod.getIterationsResults().get(n)) + "\n";
		}
		
		salida += "iterations number: " + jacobiMethod.getIterationNumber() + "\n";
		salida += jacobiMethod.isConvergent() ? "********CONVERGENT*******" : "*********DIVERGENT*******" + "\n";
	}
	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        try {
			/*
			 * 1 - Jacobi
			 * 
			 */
			int op = 1;
			switch (op){
			case 1: 
				MainActivity.jacobi();
				break;
			}
			
			TextView textview1 = (TextView)findViewById(R.id.textView1);
			textview1.setText(MainActivity.salida);
				
		} catch (Exception e){
			TextView textview1 = (TextView)findViewById(R.id.textView1);
			textview1.setText(e.toString());
		}		
    }
	
}